/*
 * Created by JFormDesigner on Sun Oct 19 18:24:23 CST 2025
 */

package com.geek.javase.ch10.jformdesigntest.other;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.*;

/**
 * @author 27636
 * 下拉菜单
 */
public class MyJComboBox extends JFrame {
    public static void main(String[] args) {
        new MyJComboBox().setVisible(true);
    }
    public MyJComboBox() {
        initComponents();
        //自己封装数据
        List<String> address = new ArrayList<>();
        address.add("安徽淮北");
        address.add("安徽合肥");
        address.add("安徽淮南");
        address.add("安徽六安");
        //封装下拉菜单的模型数据
        DefaultComboBoxModel model = new DefaultComboBoxModel(new Vector(address));
        comboBox1.setModel(model);

    }

    private void comboBox1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        int index  = comboBox1.getSelectedIndex();
        System.out.println("index = " +index);
        String string = comboBox1.getSelectedItem().toString();
        System.out.println("string = " + string);
        //设置文本域的内容
        textField.setText(string);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label = new JLabel();
        textField = new JTextField();
        comboBox1 = new JComboBox<>();

        //======== this ========
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label ----
        label.setText("\u5bb6\u5ead\u5730\u5740");
        contentPane.add(label);
        label.setBounds(new Rectangle(new Point(35, 85), label.getPreferredSize()));
        contentPane.add(textField);
        textField.setBounds(90, 145, 245, 50);

        //---- comboBox1 ----
        comboBox1.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u5b89\u5fbd\u6dee\u5317",
            "\u6c5f\u82cf\u65e0\u9521",
            "\u6e56\u5317\u6b66\u6c49",
            "\u6c5f\u82cf\u82cf\u5dde"
        }));
        comboBox1.addActionListener(e -> comboBox1ActionPerformed(e));
        contentPane.add(comboBox1);
        comboBox1.setBounds(new Rectangle(new Point(155, 75), comboBox1.getPreferredSize()));

        contentPane.setPreferredSize(new Dimension(400, 300));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label;
    private JTextField textField;
    private JComboBox<String> comboBox1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
